package com.tju.ocean_ecology_website.controller;

import com.tju.ocean_ecology_website.dto.ApiResponse;
import com.tju.ocean_ecology_website.dto.PageResult;
import com.tju.ocean_ecology_website.entity.Team;
import com.tju.ocean_ecology_website.entity.TeamMember;
import com.tju.ocean_ecology_website.entity.User;
import com.tju.ocean_ecology_website.entity.VolunteerInfo;
import com.tju.ocean_ecology_website.service.TeamMemberService;
import com.tju.ocean_ecology_website.service.TeamService;
import com.tju.ocean_ecology_website.service.UserService;
import com.tju.ocean_ecology_website.service.VolunteerInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 志愿者团队控制器
 * 注意：该控制器已被新的VolunteerTeamController替代，仅作参考
 */
// @RestController  // 注释掉该控制器，防止与VolunteerTeamController冲突
@RequestMapping("/api/volunteer/legacy")
public class VolunteerTeamsController {

    @Resource
    private UserService userService;

    @Resource
    private VolunteerInfoService volunteerInfoService;

    @Resource
    private TeamService teamService;

    @Resource
    private TeamMemberService teamMemberService;

    /**
     * 获取志愿者团队列表
     *
     * @param page 页码
     * @param pageSize 每页大小
     * @return 团队列表
     */
    @GetMapping("/teams")
    public ResponseEntity<ApiResponse<PageResult<Team>>> getVolunteerTeams(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        // 查询团队列表
        Team teamQuery = new Team();
        teamQuery.setStatus(0); // 活跃
        // 这里简化处理，实际应该在DAO层添加IN查询
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Page<Team> teamPage = teamService.queryByPage(teamQuery, pageRequest);

        // 创建分页结果
        PageResult<Team> pageResult = PageResult.fromPage(teamPage, page);

        return ResponseEntity.ok(ApiResponse.success(pageResult));
    }

    /**
     * 获取团队详情
     *
     * @param id 团队ID
     * @return 团队详情
     */
    @GetMapping("/teams/{id}")
    public ResponseEntity<ApiResponse<Team>> getTeamDetail(@PathVariable("id") Long id) {

        // 查询团队信息
        Team team = teamService.queryById(id);
        if (team == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "团队不存在"));
        }

        return ResponseEntity.ok(ApiResponse.success(team));
    }

    /**
     * 加入团队
     *
     * @param id 团队ID
     * @return 加入结果
     */
    @PostMapping("/teams/{id}/join")
    public ResponseEntity<ApiResponse<TeamMember>> joinTeam(@PathVariable("id") Long id) {
        // 查询团队信息
        Team team = teamService.queryById(id);
        if (team == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "团队不存在"));
        }

        // 检查团队状态
        if (team.getStatus() != 0) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "团队非活跃状态，无法加入"));
        }

        // 创建团队成员记录
        TeamMember member = new TeamMember();
        member.setTeamId(id);
        member.setJoinTime(new Date());
        member.setRole(2); // 普通成员
        member.setStatus(0); // 活跃
        member.setAuditStatus(0); // 待审核

        // 保存团队成员记录
        TeamMember savedMember = teamMemberService.insert(member);

        return ResponseEntity.ok(ApiResponse.success(savedMember, "加入团队成功，等待审核"));
    }

    /**
     * 退出团队
     *
     * @param id 团队ID
     * @return 退出结果
     */
    @PostMapping("/teams/{id}/quit")
    public ResponseEntity<ApiResponse<Boolean>> quitTeam(@PathVariable("id") Long id) {
        // 查询团队信息
        Team team = teamService.queryById(id);
        if (team == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "团队不存在"));
        }

        // 更新团队成员数量
        team.setMemberCount(team.getMemberCount() - 1);
        teamService.update(team);

        return ResponseEntity.ok(ApiResponse.success(true, "退出团队成功"));
    }


}
